/*
 * Primatives.java
 *
 * Created on 29 November 2007, 20:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package googlecode.menagerie.axis2.pojo;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sowen
 */
public class PrimativesService {

    private Map<String, Long> timeMap = new HashMap<String, Long>();
    private Map<String, String> jobResultMap = new HashMap<String, String>();

    /** Creates a new instance of Primatives */
    public PrimativesService() {
    }

    public int[] getIntArray() {
        int[] result = new int[5];
        for (int i = 0; i < result.length; i++) {
            result[i] = i;
        }
        return result;
    }

    public int randomDelayMilliseconds(int min,int max) throws InterruptedException {
        int diff = max-min;
        int rand = new Random().nextInt(diff);
        int delay = min + rand;
        Thread.sleep(delay);
        return delay;
    }

    public String echoWithRandomDelayMilliseconds(int min,int max, String message) throws InterruptedException {
        int diff = max-min;
        int rand = new Random().nextInt(diff);
        int delay = min + rand;
        Thread.sleep(delay);
        return message;
    }

    public String[] getStringArray() {
        return new String[]{"one", "two", "three", "four", "five"};
    }

    public int add(int a, int b) {
        return a + b;
    }

    public String makeString(int length) {
        char [] chars = new char[length];
        for (int i=0;i<length;i++) chars[i]='a';
        return String.valueOf(chars);

    }

    public void noResponse() {
    }

    public int getThree() {
        return 3;
    }

    public boolean wait(int lengthInSeconds) throws InterruptedException {
        try {
            Thread.sleep(lengthInSeconds * 1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(PrimativesService.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        return true;
    }

    public byte[] stringToByteArray(String str) {
        return str.getBytes();
    }

    public String byteArrayToString(byte[] bytes) {
        return String.valueOf(bytes);
    }

    public String echo(String msg) {
        return msg;
    }

    public char[] stringToCharArray(String str) {
        return str.toCharArray();
    }

    public String returnsNull() {
        return null;
    }

    public String startJob(int duration) {
        String uuid = UUID.randomUUID().toString();
        timeMap.put(uuid, new Long(System.currentTimeMillis() + (duration * 1000)));
        return uuid;
    }

    public String pollJob(String jobID) {
        Long time = timeMap.get(jobID);
        if (time == null) {
            return "NOJOB";
        }
        if (time > System.currentTimeMillis()) {
            return "RUNNING";
        }
        jobResultMap.put(jobID, "result_for_" + jobID);
        return "COMPLETE";
    }

    public String jobResult(String jobID) throws Exception {
        String result = jobResultMap.get(jobID);
        if (result == null) {
            return "NORESULT";
        }
        timeMap.remove(jobID);
        jobResultMap.remove(jobID);
        return result;
    }
}
